function [Val,V]=Fun_V(PP,SS,T,Profit,TAU,ir_dom,ir_ext,w_N,w_H,PortfolioInfo,UCoefp,State,Flag_SS)
% T=SS.T;Profit=SS.Profit;ir_dom=SS.ir_dom;ir_ext=SS.ir_ext;Pir=SS.Pir;
% w_N=SS.w_N;w_H=SS.w_H;State=SS.FunApp.V.State;TAU=SS.TAU;UCoefp=UCoef_0;
% PortfolioInfo=SS.PortfolioInfo;
if nargin<=12
    Flag_SS     =   0;
end
%% Preliminary
% States
N_State     =   size(State,1);
f           =   State(:,1);
ID          =   State(:,2);

ID_z        =   SS.FunApp.ExoState.State(ID,1);
ID_FI       =   SS.FunApp.ExoState.State(ID,2);
ID_RI       =   SS.FunApp.ExoState.State(ID,3);

z           =   PP.ExoState.IdioInc.Node(ID_z);
FI          =   PP.ExoState.Idio_FI.Node(ID_FI);
RI          =   PP.ExoState.Idio_RI.Node(ID_RI);

what        =   (1-TAU)*( RI*w_H + (1-RI)*w_N );

Eff_z       =   z.*(1+PP.WGap_FI(FI+1)).*(1+PP.WGap_RI(RI+1));
Eff_w       =   what.*Eff_z;
% Return rates for fin. integrated hhs
PortfolioMat=   reshape(PortfolioInfo,[4,2]);

DomShare_Pos=   PortfolioMat(1,1);
DomShare_Neg=   PortfolioMat(1,2);

ER_Pos      =   PortfolioMat(2,1);
ER_Neg      =   PortfolioMat(2,2);

KAPPA_Pos   =   PortfolioMat(3,1);
KAPPA_Neg   =   PortfolioMat(3,2);

FR_Pos      =   PortfolioMat(4,1);
FR_Neg      =   PortfolioMat(4,2);

ret_pos     =   FI*ER_Pos + (1-FI)*ir_dom;
ret_neg     =   FI*ER_Neg + (1-FI)*ir_dom+PP.KAPPA;

fc_pos      =   FI*KAPPA_Pos + (1-FI)*0;
fc_neg      =   FI*KAPPA_Neg + (1-FI)*0 + PP.KAPPA;

domshare_pos=   FI*DomShare_Pos + (1-FI)*1;
domshare_neg=   FI*DomShare_Neg + (1-FI)*1;

fr_pos      =   FI*FR_Pos + (1-FI)*ir_dom;
fr_neg      =   FI*FR_Neg + (1-FI)*ir_dom;
% Boundary for bp Imposed by Approximation Structure
fp_lb       =   SS.FunApp.U.UnitLimit(1,1);
fp_ub       =   SS.FunApp.U.UnitLimit(1,2);
% Important Constant
wN_SS       =   SS.w_N*SS.N_N+SS.w_H*SS.N_H;
x           =   T+Profit*z+f;

%% Determine the Potential UI
% Maximium Available Expenditure
b_lb        =   max(fp_lb./(1+ret_pos*(fp_lb>0)+ret_neg*(fp_lb<0)),PP.b_lb);
b_ub        =   max(fp_ub./(1+ret_pos*(fp_ub>0)+ret_neg*(fp_ub<0)),b_lb);
MaxExp      =   x-b_lb;
% Minimum Required Expenditure
[~,MinExp,~]=   UtilityFlow_Sep(PP.NU_C,PP.NU_L,SS.PSI,Eff_w,PP.MinCon*wN_SS,'Consumption');
MinExp      =   MinExp/wN_SS;
% Required UI Benefit
UI          =   (1+PP.UI_Slack)*max(MinExp-MaxExp,0);


%% Optimization
% Effective State: Available Wealth (x=c+f)
x           =   x+UI;
% Boundary 
Exp_lb      =   max(x-b_ub,MinExp);
Exp_ub      =   max(x-b_lb,Exp_lb);

[~,c_lb,~]  =   UtilityFlow_Sep(PP.NU_C,PP.NU_L,SS.PSI,Eff_w,Exp_lb*wN_SS,'Expenditure');
[~,c_ub,~]  =   UtilityFlow_Sep(PP.NU_C,PP.NU_L,SS.PSI,Eff_w,Exp_ub*wN_SS,'Expenditure');

c_lb        =   c_lb/wN_SS;
c_ub        =   c_ub/wN_SS;

% Optimization
TempFun     =   @(Policy,State) ...
                Fun_V_Eval(PP,SS,UCoefp,State,Policy,0);
TempState   =   [x,ID,ret_pos,ret_neg,what];
[Opt_c,~]   =   VecFun_GoldenSearch(TempFun,TempState,c_lb,c_ub,1e-6);
  
[Val,V]     =   Fun_V_Eval(PP,SS,UCoefp,TempState,Opt_c,Flag_SS);
%% Collect the Extra Policy
V.Policy.FlagBC ...
            =   (V.Policy.c-c_ub)<1e-6;
V.Policy.UI =   UI;
V.Policy.FlagUI ...
            =   UI>0;

Ind_Pos     =   V.Policy.b_fc>=0;
Ind_Neg     =   ~Ind_Pos;
V.Policy.b  =   V.Policy.b_fc ./ (1+ Ind_Pos.*fc_pos + Ind_Neg.*fc_neg );
V.Policy.fc =   V.Policy.b_fc - V.Policy.b;
V.Policy.b_dom= V.Policy.b .* ( Ind_Pos.*domshare_pos + Ind_Neg.*domshare_neg );
V.Policy.b_ext= V.Policy.b - V.Policy.b_dom;
V.Policy.bp_dom=V.Policy.b_dom*(1+ir_dom);
V.Policy.bp_ext=V.Policy.b_ext*(1+ir_ext);

